<template>
	<view class="pro">
		<cu-custom :isBack="true" bgColor="bg-gradual-blue">
			<block slot="backText">返回</block>
			<block slot="content">商品详情</block>
		</cu-custom>	
		<block v-if="loaded">
		    <product-img :ProductImg="ProductImg"></product-img>
			<product-body @select="selectAttrs" :shopInfo="shopInfo" :RemainScore="remain_score" :Active="active" :coupon="CouponList" :evaluateList="EvaluateList" :ProductBody="ProductBody" :attr="Attributes" :attrPrice="AttrPrice"></product-body>
			<add-tools :shopInfo="shopInfo" :ProInfo="ProductBody" :active="active"></add-tools>
		</block>
	</view>
</template>

<script>
	import { ProductInfo, EvaluateList, Coupon, getProActive } from '@/common/api'
	import { OnlinePro, OnlineAttr, ProductInt } from '@/common/api/shop'
	import ProductImg from './components/productimg'
	import ProductBody from './components/productbody'
    import AddTools from './components/tools'
	import { mapGetters } from 'vuex'
	export default {
		name: 'productInfo',
		components: {
			ProductImg,
		 	ProductBody,
            AddTools
		},
		data() {
			return {
				shopInfo: {},
				loaded: false,
				ProductImg: [],
				ProductBody: {},
				Attributes: [],
				AttrPrice: [],
				EvaluateList: [],
				CouponList: [],
				remain_score: 0,
				showAttr: {
					show: false,
					type: 0
				},
				active: {},
				params: {
					product_id: 0
				},
				onParams: {
					supply_id: 0,
					id: 0
				},
				evaluate: {
					pagea: 1, //页数
					num: 1 // 获取的条数
				}
			}
		},
		computed: {
			...mapGetters([
				'userInfo',
				'shop'
			])
		},
		watch: {
			userInfo (val, oldval) {
				this.getActive()
			}
		},			
		onLoad(opt) {
			if (this.shop.type === 5) {
				this.params.product_id = opt.pid
			} else if (this.shop.type === 1) {
				this.onParams.supply_id = opt.supply_id
				this.onParams.id = opt.pid
			}
			this.getInfo()
		},
		methods: {
			selectAttrs(args) {
				let k = this.Attributes[args[0]].parent_pros[args[1]]
				this.Attributes[args[0]].parent_pros.forEach(item => {
					if (item.id !== k.id) item.checked = false
				})
				k.checked = !k.checked
			},			
		async getInfo () {
			uni.showLoading()
			let res = false
			if (this.shop.type === 5) { 
				res = await this.productInfo() // 如果获取产品信息失败
			} else if (this.shop.type === 1) {
				let result = await this.getOnlinePro()
				if (result) {
					res = await this.onlineProInfo()
				}
			}
			if (res) {
				await this.getEvaluate()
				if (this.shop.type === 5) await this.getCoupon()
				if (this.userInfo) await this.getActive()
				this.loaded = true
				uni.hideLoading()
			  } else {
				  uni.hideLoading()
				  uni.showModal({
					  content: '产品信息加载失败',
					  showCancel: false,
					  success () {
						  uni.navigateBack()
					  }
				  })
			  }
			},
			productInfo () { // 线上商场获取商品信息 sho.type === 5
				return new Promise((resolve, reject) => {
					ProductInfo(this.params).then(res => {
						if (!res.data.createtime) resolve(false)
						this.ProductImg = res.product_img
						this.ProductBody = res.data
						this.remain_score = res.remain_score
						this.AttrPrice = res.product_prices
						try {
							this.Attributes = res.pros_data.map(item => { 
								item.parent_pros = item.parent_pros.map(x => {
									  x.checked = false
									  return x
								})
								return item
							})
						} catch (err) {
							let list = []
							for(let i in res.pros_data) list.push(res.pros_data[i])
							this.Attributes = list.map(item => { 
								item.parent_pros = item.parent_pros.map(x => {
									  x.checked = false
									  return item
								})
								return item
							})
							console.info('item.parent_pros is not an Array')
						}
						resolve(true)
					}).catch(err => {
						uni.showModal({
							content: '无法获取产品信息',
							showCancel: false,
							success() {
								uni.navigateBack()
							}
						})
					})
				})
			},
			getOnlinePro () { // 线下商场产品信息 sho.type === 1
				return new Promise(resolve => {
					OnlinePro(this.onParams).then(res => {
						let item = {
							data: {},
							product_img: [],
							product_prices: [],
							pros_data: [],
							remain_score: 0
						}
						item.data.bad_level = 0
						item.data.bug_isvoice = 0
						item.data.bug_tips = ''
						item.data.collect = 0
						item.data.createtime = ''
						item.data.customer_service = ''
						item.data.default_imgurl = res.default_imgurl
						item.data.description = res.description
						item.data.good_level = 0
						item.data.id = res.id
						item.data.introduce = res.introduce
						item.data.name = res.name
						item.data.need_score = 0
						item.data.now_price = res.now_price
						item.data.orgin_price = res.orgin_price
						item.data.ratio = 0
						item.data.remarks = ''
						item.data.sell_count = res.sell_count
						item.data.show_sell_count = res.sell_count
						item.data.storenum = res.storenum
						item.data.supply_id = res.supply_id
						item.data.tol_level = 0
						item.data.type_id = res.type_ids
						item.data.isvalid = res.isvalid
						res.banner_imgurl.length ? item.product_img = res.banner_imgurl : item.product_img.push(res.default_imgurl)
						item.data.checked = true
						item.data.property_id = ''
						item.data.prope = ''						
						OnlineAttr({ pid: this.onParams.id }).then(x => {
							item.product_prices = x.product_prices
							item.pros_data = x.pros_data
							this.ProductImg = item.product_img.map(item => {
								let o = {}
								if (!res.banner_imgurl.length) {
									o.imgurl = item
								} else {
									o.imgurl = item.banner_imgurl
								}
								return o
							})
							this.ProductBody = item.data
							this.remain_score = item.remain_score
							this.AttrPrice = x.product_prices.map(item => {
								item.need_score = 0
								return item
							})
						try {
							this.Attributes = item.pros_data.map(item => {
								item.parent_pros = item.parent_pros.map(x => {
									  x.checked = false
									  x.name = x.pro_name
									  return x
								})
								return item
							})
						} catch (err) {
							let list = []
							for(let i in item.pros_data) list.push(item.pros_data[i])
							this.Attributes = list.map(item => { 
								item.parent_pros = item.parent_pros.map(x => {
									  x.checked = false
									  x.name = x.pro_name
									  return x
								})
								return item
							})
							console.info('item.parent_pros is not an Array')
						}							
							resolve(true)
						})
					})
				})
			},
			onlineProInfo () {
				return new Promise(resolve => {
					ProductInt(this.onParams).then(res => {
						this.shopInfo = res.shop_detailed.filter(item => item.id === +this.onParams.supply_id)[0]
						resolve(true)
					})
				})
			},
			getEvaluate () { // 获取评论
				return new Promise(resolve => {
					EvaluateList({...this.params, ...this.evaluate}).then(res => {
						if (res.length) {
							this.EvaluateList = res.map(item => {
								item.discussimg = item.discussimg.filter(m => m)
								item.weixin_name = item.weixin_name ? item.weixin_name : '匿名用户'
								item.weixin_headimgurl = item.weixin_headimgurl ? item.weixin_headimgurl : '/static/avatar.png'
								return item
							})
						}
						resolve()
					})					
				})
			},
			getCoupon () {  // 获取优惠券
				return new Promise(resolve => {
					Coupon(this.params).then(res => {
						if (!res.code) this.CouponList = res.message
						resolve()
					})
				})
			},
			getActive () { // 获取活动
			    return new Promise(resolve => {
					let c = {}
					if (this.shop.type === 5) {
						c.pid = this.params.product_id
					} else if (this.shop.type === 1) {
						c.pid = this.onParams.id
					}
					getProActive(c).then(res => {
						this.active = res.data
						resolve()
					})					
				})
			}
		}
	}
</script>

<style lang="less">
	.pro { overflow: hidden }
</style>